package com.yanxinim.cn.store.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yanxinim.cn.R;
import com.yanxinim.cn.store.bean.ProductType;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class GoodsImageAdapter extends RecyclerView.Adapter<GoodsImageAdapter.ViewHolder> {
    private Context context;
    private List<String> dataList = new ArrayList<>();
    private ItemOnClickListener itemOnClickListener;
    private int selectedPosition = 0;

    public void setItemOnClickListener(ItemOnClickListener itemOnClickListener) {
        this.itemOnClickListener = itemOnClickListener;
    }

    public GoodsImageAdapter(Context context, List<String> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    public void addAllData(List<String> dataListUpdate) {
        if (dataListUpdate != null && dataListUpdate.size() > 0) {
            dataList.addAll(dataListUpdate);
            notifyItemRangeChanged(dataList.size() - dataListUpdate.size(), dataListUpdate.size());
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.goods_image_adapter, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
        Glide.with(context).load(dataList.get(position)).into(viewHolder.imageView);
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView imageView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.iv_goods_img);
        }
    }


    public interface ItemOnClickListener {
        void onItemClick(ProductType newType, int position);
    }
}
